#!/usr/bin/env python


import os
import re
import time

t=int(time.time())
flow_path=""
work_dir=""
a_dir=""
server_ip=""

def check_dir(dir):
    if os.path.isdir(dir):
        #return 1
        pass
    else :
        os.makedirs(dir)
        #return -1

def getfile():
    global t,flow_path,work_dir,a_dir
    e_path=os.path.join(work_dir,"csv")
    check_dir(e_path)
    tp=time.localtime(t)
    path_str="%Y/%Y-%m/%Y-%m-%d/"
    #/root/flow_new/2012/2012-01/2012-01-03
    path=os.path.join(flow_path,time.strftime(path_str,tp))
    f_list=os.listdir(path)
    f_list=f_list.sort()
    f_name=""
    for i in xrange(1,len(f_list)):
        n=f_list[-i]
        if n[:2]=="ft":
            f_name=os.path.join(path,n)
            break
        else :
            continue
    if f_name=="" :
        print "error! not available file!"
        return -1
    e_name=os.path.join(e_path,n+".csv")
    cmd="flow-cat %s | flow-export -f2 > %s"%(f_name,e_name)
    os.popen(cmd)
    #mv processed file
    a_path=os.path.join(a_dir,time.strftime(path_str,tp))
    check_dir(a_path)
    cmd="mv -f %s %s"%(f_name,a_path)
    os.popen(cmd)

    return e_name

def process_data(lines):
    #return method,list
    #method -1: error   1  out,  0 in
    #list   -1 "error line data is not 24"      -2  "not find server ip"
    global server_ip
    line=lines.split(",")
    ##:unix_secs,unix_nsecs,sysuptime,exaddr,dpkts,doctets,first,last,engine_type,engine_id,srcaddr,dstaddr,nexthop,input,output,srcport,dstport,prot,tos,tcp_flags,src_mask,dst_mask,src_as,dst_as
    #1325641320,941000,1048580997,127.0.0.1,26,1872,1048561661,1048575947,0,0,68.148.119.111,74.63.111.242,0.0.0.0,0,0,2140,22,6,0,24,0,0,0,0
    #0:unix_secs
    #4:dpkts  packages number
    #5:doctets  byte
    #10:srcaddr
    #11:dstaddr
    #15:srcport
    #16:dstport
    #17:prot  tcp=17
    if len(line)!=24:
        print "error!!! \n#######%s######"%lines
        return -1,-1
    else:
        #result={}
        result={'packets': int(line[4]), "octets" : int(line[5])}
        if line[10]==server_ip:
            result["ip"]=line[11]
            return 0,result
            #in
        elif line[11]==server_ip:
            result["ip"]=line[10]
            return 1,result
            #out
        else:
            return -1,-2

def main():
    global t
    file_path=getfile()
    if file_path =="-1":
        sys.exit()
    else:
        database={}
        ip_list=[]
        for i in open(file_path):
            if i[:1]=="#":
                continue
            method , data=process_data(i)
            if method =="-1":
                print "error  line not found server ip!  \n######%s#######"%i
                continue
            elif method == 0:
                mode="in"
            elif method == 1 :
                mode="out"
            else:
                print "unknown error"
                continue

            if data["ip"] not in ip_list:
                database[data["ip"]]={"in":{ "packets":0, "octets":0 },
                          "out":{ "packets":0, "octets":0 }}
                ip_list.append(data["ip"])

            database[data["ip"]][mode]["packets"]+=data["packets"]
            database[data["ip"]][mode]["octets"]+=data["octets"]
    #delete tmp files
    cmd="rm -f %s"% file_path
    os.popen(cmd)
    print database
